//
//  SSHomeTableViewCell.swift
//  weibo
//
//  Created by 邵帅 on 16/6/27.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit
import SnapKit

class SSHomeTableViewCell: UITableViewCell {
    
    var bottomConstraint: Constraint?
    

    var model: SSStatusCellModel?{
        // 已经赋值
        didSet{
            
            orginalView.orginalViewModel = model
            
            footerView.footerViewModel = model
            
            bottomConstraint?.uninstall()
            
            if model?.model?.retweet_status == nil {
                
                retweetView.hidden = true
                
                footerView.snp_updateConstraints(closure: { (make) in
                  
                    self.bottomConstraint = make.top.equalTo(orginalView.snp_bottom).constraint
                })
            }else {
                
                retweetView.hidden = false
                
                footerView.snp_updateConstraints(closure: { (make) in
                    
                    self.bottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                retweetView.retweetModel = model
            }
            
        }
        
    }
    
    // 第一步 -- 重写构造函数
    // cell 的指定构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 第二步 -- 定义一个方法,来实现 视图的添加和布局
    private func setupUI(){
        
        contentView.addSubview(orginalView)
        contentView.addSubview(footerView)
        contentView.addSubview(retweetView)
        
        // 设置约束
        orginalView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
            
            // 设置底部
            make.bottom.equalTo(retweetView.snp_top).offset(-5)
            
        }
        
        retweetView.snp_makeConstraints { (make) in
            
            make.left.equalTo(contentView).offset(10)
            
            make.right.equalTo(contentView).offset(-10)
        }
        
        
        // 自动计算行高的原理同学们要领会
        footerView.snp_makeConstraints { (make) -> Void in
            
            self.bottomConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            
            
        }
        
        // 需要设置contentView的左上右下,特别是下边
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            
            // 最最重要的是底部是哪里
            make.bottom.equalTo(footerView.snp_bottom)
            
        }
        
    }
    
    // 第三步: 懒加载控件
    private lazy var orginalView: SSOrginalView = SSOrginalView()
    
    private lazy var footerView: SSToolBarView = SSToolBarView()
    
    private lazy var retweetView: SSRetweetView = SSRetweetView()
    
}
